package com.manong.web.controller;

import com.github.pagehelper.PageInfo;
import com.manong.data.ResultData;
import com.manong.domain.Warehouse;
import com.manong.query.WarehouseQueryObject;
import com.manong.service.WarehouseService;
import com.manong.util.JsonResult;
import com.manong.util.RequiredPermission;
import com.manong.vo.GoodsWarehouseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/warehouse")
@CrossOrigin(allowCredentials = "true")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    /**
     * 查询仓库列表
     * @param queryObject
     * @return
     */
    @GetMapping("/list")
    @RequiredPermission(name = "查询仓库列表", expression = "warehouse:getList")
    public JsonResult list(WarehouseQueryObject queryObject) {
        PageInfo pageInfo =  warehouseService.query(queryObject);
        ResultData data = new ResultData(queryObject.getPageNum(),queryObject.getPageSize(),pageInfo.getList(),Integer.parseInt(pageInfo.getTotal()+""));
        return JsonResult.success(data);
    }

    /**
     * 删除仓库接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiredPermission(name = "仓库删除", expression = "warehouse:delete")
    public JsonResult delete(@PathVariable Integer id) {
        warehouseService.delete(id);
        return JsonResult.success();
    }


    /**
     * 新增或者修改仓库
     * @param warehouse
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @RequiredPermission(name = "仓库新增或者编辑", expression = "warehouse:saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody Warehouse warehouse) {
        warehouseService.saveOrUpdate(warehouse);
        return JsonResult.success();
    }

    /**
     * 查询单个仓库
     * @param id
     * @return
     */
    @GetMapping("/getWarehouse/{id}")
    @RequiredPermission(name = "查询单个仓库", expression = "warehouse:getwarehouse")
    public JsonResult getwarehouse(@PathVariable Integer id) {
        Warehouse warehouse = warehouseService.get(id);
        // JsonResult data属性 role
        return JsonResult.success(warehouse);
    }

    /**
     * 获取全部仓库数量
     * @return
     */
    @GetMapping("/totalWarehouseCount")
    @RequiredPermission(name = "查询全部仓库数量", expression = "warehouse:totalWarehouseCount")
    public JsonResult totalWarehouseCount() {
        int totalWarehouseCount = warehouseService.getTotalWarehouseCount();
        return JsonResult.success(totalWarehouseCount);
    }

    /**
     * 根据 manager 查询仓库的 id, capacity, 和 used_space
     * @param manager
     * @return
     */
    @GetMapping("/getWarehousesByManager/{manager}")
    @RequiredPermission(name = "根据 manager 查询仓库", expression = "warehouse:getWarehousesByManager")
    public JsonResult getWarehousesByManager(@PathVariable Integer manager) {
        List<Warehouse> warehouses = warehouseService.getWarehousesByManager(manager);
        return JsonResult.success(warehouses);
    }

    /**
     * 查询仓库中现存商品名称及其对应的数量
     * @param warehouseId
     * @return
     */
    @GetMapping("/getGoodsInWarehouse/{warehouseId}")
    @RequiredPermission(name = "查询仓库中现存商品名称及其对应的数量", expression = "warehouse:getGoodsInWarehouse")
    public JsonResult getGoodsInWarehouse(@PathVariable Integer warehouseId) {
        List<GoodsWarehouseVo> goodsInWarehouse = warehouseService.getGoodsInWarehouse(warehouseId);
        return JsonResult.success(goodsInWarehouse);
    }
}
